<?php
namespace app\index\controller;

use app\index\Controller\Base;
use think\Db;
class Member extends Base
{
	//佣金记录	
	public function yj_record(){
		//开卡人数
		$uid = session('user_info')['id'];
		$count = db('commission')->where('ywy_id',$uid)->where('type',1)->count('id');
		$this->assign('count',$count);
		//开卡金额
		$balance = db('commission')->where('ywy_id',$uid)->where('type',1)->sum('balance');
		$this->assign('balance',$balance);
		if($_POST){ 
		    $list = db('commission')->alias('c')
		          ->join('user u','u.id = c.user_id','LEFT')
				  ->join('userlevel s','s.id = c.level_id','LEFT')
		          ->where('c.ywy_id',$uid)
				  ->where('c.type',1)
				  ->field('c.*,u.user_name,s.name')
		          ->order('c.add_time desc')->page(input("page"),10)->select();
			return json(["list"=>$list]);
		}
		return $this->fetch();
	}	
	//我的会员	
	public function friends_list(){
		if($_POST){
			$uid = session('user_info')['id'];
		    $list = db('user')->where('tj_emp',$uid)->field('logo,nickname,mobile')->order('time desc')->page(input("page"),10)->select();
			return json(["list"=>$list]);
		}
		return $this->fetch();
	}
    public function index()
    {
       $user=db('user')
       ->alias('a')->join('userlevel b','b.id=a.level_id',"LEFT")
	   ->field("a.*,b.name")
       ->where('openid',session('openid'))->find();
	   $this->assign('user',$user);	
       return $this->fetch();
    }
	//客房预定
	public function houseyd(){
		return $this->fetch();
	}
	//房券
	public function quan(){
		//查看个人信息
		$user=db('user')
       ->alias('a')->join('userlevel b','b.id=a.level_id',"LEFT")
	   ->field("a.kaquan,a.id,a.logo,b.name")
       ->where('openid',session('openid'))->find();
	   $this->assign('user',$user);	
	    
		//查询出这个人所有赠送的房券
		$uid = session('user_info')['id'];
		$where['u.user_id'] = $uid;
		$where['u.start_time'] = ['<=',date("Y-m-d")];
		$where['u.num'] = ['neq',0];
		$list = db('userhouse')->alias('u')
		->join('room_sort h','u.house_id = h.id','LEFT')
		->field('u.*,h.room_name')
		->where($where)->select();
		$this->assign('list',$list); 
		$this->assign('mobile',session('user_info')['mobile']);
		
		//待激活房券
		$count = db('userhouse')->where('start_time','>',date("Y-m-d"))->where('user_id',$uid)->field('sum(num) as num1,user_id')->buildSql();
		//已激活房券
		$count2 = db('userhouse')->where('start_time','<=',date("Y-m-d"))->where('user_id',$uid)->field('sum(num) as num2,user_id')->buildSql();
		$total = db('paylog')->alias('p')
		->join("$count c",'c.user_id = p.user_id','LEFT')
		->join("$count2 c2",'c2.user_id = p.user_id','LEFT')
		->field('p.add_time,c.num1,c2.num2')
		->where('p.type',1)
		->where('p.user_id',$uid)
		->find(); 
		$this->assign('total',$total);  
			
        //明细列表
        $type = input('type') ? input('type') : 4;
		$wheres['c.user_id'] = $uid;
		$wheres['c.type'] = $type;
		if($type == 3){
			$wheres['c.fill_id'] = ['neq',''];
		} 
		$fqlist = db('consume')->alias('c') 
		->join('user u','u.id = c.fill_id','LEFT')
		->join('store t','t.id = c.store_id','LEFT')
		->field('c.order_sn,c.time,u.mobile,t.store_name')
		->group('c.order_sn')
		->order('c.time desc')
		->where($wheres)->select();
		foreach($fqlist as $k=>$v){  
			$sn = $fqlist[$k]['order_sn'];
			$fqlist[$k]['fq'] = db('consume')->alias('c')
			->join('userhouse h','h.id = c.uh_id','LEFT')
			->join('room_sort s','h.house_id = s.id','LEFT') 
			->field('c.num,c.room_name,s.room_name')
			->order('c.time desc')
			->where("c.order_sn = '{$sn}'")->where($wheres)->select(); 
		}   
		$this->assign('type',$type);
		$this->assign('fqlist',$fqlist);
		return $this->fetch();
	}

    public function zquan()
	{
		if($_POST){
			$uid = session('user_info')['id'];
			$result = true;
			Db::startTrans();
			//验证验证码是否错误，过期
			$result = check_sendcode(session('user_info')['mobile'], $_POST['yzm'], '发送成功'); 
			if($result != 1) {
				return json(['code'=>400,"msg"=>'验证码错误或过期']);
			}  
			$mobile = input('mobile');
			$num = $_POST['num'];
			$id = $_POST['id'];
			//判断该用户是否存在
			$user = db('user')->where('mobile',$mobile)->select();
			if(!$user){
				return json(['code'=>400,"msg"=>'该转入会员不存在']);
			}
			
			$order_sn = order_create_no();
			$time = now_time();
			foreach($id as $k=>$v){
				//对方ID
				$did = db('user')->where('mobile',$mobile)->value('id');
				//判断房券次数是否足够
				$info = db('userhouse')->where('id',$v)->find(); 
				if($info['num'] < $num[$k]){
					return json(['code'=>400,"msg"=>'转赠失败，卡券不足']);
				}
				if(!empty($num[$k])){
					//判断对方是否有这个房券
					$df['user_id'] = $did;
					$df['house_id'] = $info['house_id'];
					$house = db('userhouse')->where($df)->select();
					if($house){
						//存在就修改数量
						//对方房券增加
					    $res = db('userhouse')->where($df)->setInc('num', $num[$k]);
						if(!$res) $result = false;
					}else{
						//不存在就添加一条数据
						$data['user_id'] = $did;
						$data['house_id'] = $info['house_id'];
						$data['num'] = $num[$k];
						$data['start_time'] = $info['start_time'];
						$data['end_time'] = $info['end_time'];
						$data['type'] = 1;
						$res2 = db('userhouse')->insert($data);
						if(!$res2) $result = false;
					}
					
					//对方房券增加
					$res3 = db('user')->where('id',$did)->setInc('kaquan',$num[$k]);
					if(!$res3) $result = false;
					//自己房券减
					$res4 = db('user')->where('id',$uid)->setDec('kaquan',$num[$k]);
					if(!$res4) $result = false;
					
					//自己房券减
					$res5 = db('userhouse')->where('id',$v)->setDec('num', $num[$k]);
					if(!$res5) $result = false; 
					
					//打印转增日志 
				    $res6 = $this->fqlog(5,$uid,$did,$v,$num[$k],'房券转赠',$order_sn,$time);
					if(!$res6) $result = false;
					//打印获增日志 
				    $res7 = $this->fqlog(3,$did,$uid,$v,$num[$k],'房券获赠',$order_sn,$time);
					if(!$res7) $result = false;
				}
				
			}
            if($result){
				Db::commit();
				return $this->success('转赠成功'); 
			}else{
				Db::rollback();
				return $this->error('转赠失败');
			}
		}
	}

    public function fqlog($type,$user_id,$fill_id,$uh_id,$num,$msg,$order_sn,$time)
	{
		//打印转增日志 
	    $datas['type'] = $type;
		$datas['order_sn'] = $order_sn;
		$datas['user_id'] = $user_id;  
		$datas['fill_id'] = $fill_id;  //对方用户ID
		$datas['uh_id'] = $uh_id;
		$datas['num'] = $num;
		$datas['time'] = $time;
		$datas['msg'] = $msg;
		$res = db('consume')->insert($datas);
		return $res;
	}
	//会员卡
	public function card(){
		$info_user = session('user_info');
		$no_time=$info_user['no_time'];
		if($no_time){
			$res=timediff(strtotime($no_time),time());
			if($res['min']>15){
				$this->update_card($info_user);
			}
			
		}else{
			$this->update_card($info_user);
		}
		return $this->fetch();
	}
	//更新会员动态码
	private function update_card($user){
		$no=order_create_no();
		$time=now_time();
		$res=db("user")->where("id",$user['id'])->update(['no_time'=>now_time(),'no'=>$no]);
		$user['no']=$no;
		$user['no_time']=$time;
		session("user_info",$user);		
	}
	//购买会员卡
	public function buycard(){
		if($_POST){
			$info_user = session('user_info');
			$level=db("userlevel")->where("id",input('level_id'))->find();
			$cz_much=$level['price'];
			$pay_type=get_brower_type();//判断是微信还是支付宝打开
			$order_sn = order_create_no();
			$rest=recharge_log(0,$info_user['id'],$level['price'],1,$order_sn,$pay_type,'购买会员卡',1,$level['id']);
			if($pay_type==1){//支付宝
		        $url=pay_code('H480','ea4f13f136c95a617b1acfbda7af6eb1','45a7a757b685f5c890b815839e58ca18',$cz_much,1,$order_sn);
			}elseif ( $pay_type==2 ) {//微信扫码
				$url=pay_code('H480','ea4f13f136c95a617b1acfbda7af6eb1','45a7a757b685f5c890b815839e58ca18',$cz_much,2,$order_sn);
			}
			Header("Location: $url");
			exit;
		}
		$level=db('userlevel')->select();
		$this->assign("level",$level);
		return $this->fetch();
	}
	 
	//充值
	public function czcard(){
		if($_POST){
			$info_user = session('user_info');
			$fill=db("fill")->where("id",input('cz_id'))->find();
			$cz_much=$fill['money'];
			$pay_type=get_brower_type();//判断是微信还是支付宝打开
			$order_sn = order_create_no();
			$rest=recharge_log(0,$info_user['id'],$fill['money'],2,$order_sn,$pay_type,'会员卡充值',1,$fill['id']);
		
			if($pay_type==1){//支付宝
		        $url=pay_code('H480','ea4f13f136c95a617b1acfbda7af6eb1','45a7a757b685f5c890b815839e58ca18',$cz_much,1,$order_sn);
			}elseif ( $pay_type==2 ) {//微信扫码
				$url=pay_code('H480','ea4f13f136c95a617b1acfbda7af6eb1','45a7a757b685f5c890b815839e58ca18',$cz_much,2,$order_sn);
			}
			Header("Location: $url");
			exit;
		}
		//查看个人信息
		$user=db('user')
       ->alias('a')->join('userlevel b','b.id=a.level_id',"LEFT")
	   ->field("a.balance,a.id,a.logo,b.name")
       ->where('openid',session('openid'))->find();
	    $this->assign('user',$user);	
		$list=db('fill')->select();
		$this->assign("list",$list);
		return $this->fetch();
	}
	public function friends()
    {
    	//查询我的会员人数
    	$user_id = session('user_info')['id'];
    	$count = db('user')->where('tj_emp',$user_id)->field('tj_emp,count(id) as count')->buildSql();
		$user = db('user')->alias('u')
		->join("$count c",'u.id = c.tj_emp','LEFT')
		->field('u.yj,c.count')
		->where('u.id',$user_id)
		->find();
		//累积佣金收入
		$ywy_yj = db('commission')->where('ywy_id',$user_id)->where('type',1)->sum('ywy_yj');
		$this->assign('ywy_yj',$ywy_yj); 
		$this->assign('user',$user); 
		
		$code_url="http://".$_SERVER['HTTP_HOST']."/index/index/index?tj_emp=".$user_id;
		$this->assign('code_url',$code_url);
        return $this->fetch();
    }
	
	//设置
	public function memset()
    {
       return $this->fetch();
    }
	
	/**
	 * 客房预订
	 * */
	public function reserve(){
		$status = input('status')==''?0:input('status'); 
		$this->assign('type',$status);
		if($_POST){
			$user_id = session('user_info')['id'];
			$status = input('type')==''?0:input('type');
			//dump($status);die;
			if($status == 0){
				$where = [];
			}else{
				$where['type'] = $status;
			}
			$list = db('reserve')->alias('a')
			->join('room_sort b','a.room_id=b.id','LEFT')
			->join('store c','a.store_id=c.id','LEFT')
			->field('a.*,b.room_name,c.thumb,c.store_name,b.room_img')
			->where('a.user_id',$user_id)
			->where($where)
			->order('a.add_time desc')
			->page(input("page"),10)
			->select();
			return json(["list"=>$list]);
		} 
		return $this->fetch();
	}
	/**
	 * 餐厅预订
	 * */
	public function reserve_ct(){
		if($_POST){
			$user_id = session('user_info')['id'];
			$list = db('reserve_ct')->alias('a')
			->join('hyjt_area b','a.room_id=b.id','LEFT')
			->join('store c','a.store_id=c.id','LEFT')
			->field('a.*,b.area_name,c.thumb,c.store_name')
			->where('a.user_id',$user_id)
			->order('a.add_time desc')
			->page(input("page"),10)
			->select();
			return json(["list"=>$list]);
		} 
		return $this->fetch();
	}
	//点餐
	public function diancan($r_id="",$store_id="",$room_id=""){
		$user_id = session('user_info')['id'];
		if($room_id){
			$res=db("reserve_ct")->wheretime("add_time","today")->where("user_id",$user_id)->where("room_id",$room_id)->where("status",1)->find();
			if($res){
				$r_id=$res['id'];
				$store_id=$res['store_id'];
			}else{
				$data['user_id']=$user_id;
				$data['store_id']=$store_id;
				$data['state']=3;
				$data['room_id']=$room_id;
				$data['add_time']=now_time();
				$r_id = Db::name('reserve_ct')->insertGetId($data);
			}
		}
		
		
		return $this->fetch("",["r_id"=>$r_id,"store_id"=>$store_id]);
	}
	/**
	  * 获取点餐商品和分类
	  */
	 public function getdc($store_id){
	 	 $goods=db("caipin")->where("store_id",$store_id)->select();
		 //$type=$this->getTypes($store_id);
		 $type=db('caipin_type')->select();
		 $type1=array();
		 $res=array();
		 foreach($type as $k=>$v){
				$type1[$v['id']]=$v;
		 }
		 foreach($goods as $k=>$v){
		 	$res[$v['type_id']][]=$v;
		 }
		 $data['type']=$type;
		 return json(['res'=>$res,'type'=>$type1]);
	 }
	 //添加订单详情表
	 public function add_dc($r_id,$store_id){
	 	$gds=input('')['cart'];//购物车商品列表
	 	$sum=0;
		foreach($gds as $i){
		  $sum+=$i['num']*$i['price'];
		  $items[]=array("add_time"=>now_time(),'caipin_id'=>$i['id'],"r_id"=>$r_id,"price"=>$i['price'],"name"=>$i['name'],"num"=>$i['num'],"store_id"=>$store_id);
		}
		$res=db('reserve_item')->insertAll($items);
		if($res){
			$up['origin_price']=['inc',$sum];
			$up['state']=1;
			db("reserve_ct")->where("id",$r_id)->update($up);
			return json(['code'=>200,'msg'=>'操作成功']);
		}else{
			return json(['code'=>200,'msg'=>'操作失败']);
		}
	 }
	/**
	 * 客房预订详情
	 * */
	public function reserve_det(){
		$id = input('r_id');
		$res = db('reserve')->alias('a')
				->join('room_sort b','a.room_id=b.id','LEFT')->join('store c','a.store_id=c.id','LEFT')->join('user d','a.user_id=d.id','LEFT')->join('paylog e','a.order_id=e.id','LEFT')
				->field('a.type,a.jc_time,a.is_rooms,a.is_hx,a.state,a.zf_type,a.balance,a.add_time,b.room_name,c.thumb,c.store_name,b.room_img,d.user_name,d.mobile,e.order_sn')->where('a.id',$id)->find();
		$res['pay_name'] = '';
		if($res['zf_type']==2){
			$res['pay_name'] = '微信支付';
		}elseif($res['zf_type']==5){
			$res['pay_name'] = "会员卡支付";
		}elseif($res['zf_type']==6){
			$res['pay_name'] = "卡券支付";
		}elseif($res['zf_type']==7){
			$res['pay_name'] = "到店支付";
		}
		$this->assign('vo',$res);
		return $this->fetch();
	}
	/**
	 * 餐厅预定详情
	 * */
	public function reserve_det2(){
		$id = input('r_id');
		$res = db('reserve_ct')->alias('a')
				->join('hyjt_area b','a.room_id=b.id','LEFT')
				->join('store c','a.store_id=c.id','LEFT')
				->join('user d','a.user_id=d.id','LEFT')
				->join('paylog e','a.order_id=e.id','LEFT')
				->field('a.id,a.store_id,a.type,a.origin_price,a.jc_time,a.is_rooms,a.is_hx,a.state,a.zf_type,a.balance,a.add_time,b.area_name,c.thumb,c.store_name,d.user_name,d.mobile,e.order_sn')
				->where('a.id',$id)->find();
		$res['pay_name'] = '';
		if($res['zf_type']==2){
			$res['pay_name'] = '微信支付';
		}elseif($res['zf_type']==5){
			$res['pay_name'] = "会员卡支付";
		}elseif($res['zf_type']==6){
			$res['pay_name'] = "卡券支付";
		}elseif($res['zf_type']==7){
			$res['pay_name'] = "到店支付";
		}
		$items=db("reserve_item")->where("r_id",$id)->select();
		$this->assign('items',$items);
		$this->assign('vo',$res);
		$user_id = session('user_info')['id'];
    	$user_info = db('user')->alias('a')->join('userlevel b','a.level_id=b.id','LEFT')->where('a.id',$user_id)->field('a.level_id,a.kq_hx,a.balance,a.mobile,a.user_name,b.name as level_name,b.discount,b.zs_num')->find();
		$this->assign('user_info',$user_info);
		return $this->fetch();
	}
	//点餐支付
	public function reserve_pay(){
			$user_id = session('user_info')['id'];
			$reserve_id=input("id");
	    	$user_info = db('user')->alias('a')->join('userlevel b','a.level_id=b.id','LEFT')->where('a.id',$user_id)->field('a.balance,a.level_id,a.id,a.mobile,a.user_name,b.name as level_name,b.discount')->find();
			$reserve=db("reserve_ct")->where("id",$reserve_id)->find();
			$origin_price = $reserve['origin_price'];//应付金额
			$zk=1;//折扣
			$balance =$origin_price*$zk;  //实际支付
			$yh_money = 0; //优惠金额
			//订单信息
			$order_sn = order_create_no();
			if(input('zf_type')==5){//会员卡支付
			    if($user_info['level_id']){
					$discount = db("storediscount")->where("store_name_id",$reserve['store_id'])->where("userlevel",$user_info['level_id'])->find();
					$zk=1-$discount['discount']/100;
				}
				
				$balance =$origin_price*$zk;  //实际支付
				$yh_money = $origin_price-$balance; //优惠金额
				$paylog = db('paylog')->where('order_sn',$order_sn)->find();
				if($balance>$user_info['balance']){
					return json(['code'=>400,"msg"=>'会员卡余额不足']);
				}
				Db::startTrans();
				$res=[];
				$res[]=db('user')->where('id',$user_id)->setDec('balance',$balance);  //更新用户表
				$res[]=db('reserve_ct')->where('id',$reserve_id)->update(['state'=>2,'pay_time'=>now_time()]);  //更新预定表
				$res[]=reserve_pay_log(input("store_id"),$user_info['id'],$balance,7,$order_sn,input("zf_type"),'餐厅预定',2,$user_info['level_id'],$origin_price,input("id"));
				if (in_array(false, $res)){
				    Db::rollback();
					return json(['code'=>400,"msg"=>'支付失败']);
				}else {
				    Db::commit();
					return json(['code'=>400,"msg"=>'支付成功']);
				}
			   
			}else{
				$res=reserve_pay_log(input("store_id"),$user_info['id'],$balance,7,$order_sn,input("zf_type"),'餐厅预定',1,$user_info['level_id'],$origin_price,input("id"));
			}
			
			if($_POST['zf_type']==2){//微信支付
				$url=pay_code('H506','98525211960f75494838aa9dde12af3f','600fbbf2d114ebbd04fdd0f9bec22eb8',$balance,2,$order_sn);
				return json(['code'=>202,"url"=>$url,"msg"=>'']);
			}
			
	}
	//换绑手机号
	public function mobile()
    {
    	if(request()->isPost()){
    		//验证验证码是否错误，过期
			$result = check_sendcode($_POST['mobile'], $_POST['yzm'], '发送成功'); 
			if($result != 1) {
				return json(['code'=>400,"msg"=>'验证码错误或过期']);
			}  
			$user_id = session('user_info')['id']; 
			$data['mobile'] = $_POST['mobile'];
			//判断该号码是否绑定过
			$info = db('user')->where('mobile',$_POST['mobile'])->find();
			if($info){
				return json(['code'=>400,"msg"=>'该号码已经绑定过']);
			}
			$res = db('user')->where('id',$user_id)->update($data);
			if($res){
				session("user_info",$info);
				return json(['code'=>200,"msg"=>'换绑成功']);
			}else{
				return json(['code'=>400,"msg"=>'换绑失败']);
			}
    	}
        return $this->fetch();
    }
	
	//修改支付密码
	public function update_pwd()
	{
		$user_id = session('user_info')['id']; 
		//判断该号码是否绑定过
		$mobile = db('user')->where('id',$user_id)->value('mobile');
		$this->assign('mobile',$mobile);
		if(request()->isPost()){
    		//验证验证码是否错误，过期
			$result = check_sendcode($mobile, $_POST['yzm'], '发送成功'); 
			if($result != 1) {
				return json(['code'=>400,"msg"=>'验证码错误或过期']);
			}   
			$data['password'] = md5_salt($_POST['password']);
			
			$res = db('user')->where('id',$user_id)->update($data);
			if($res){
				return json(['code'=>200,"msg"=>'修改成功']);
			}else{
				return json(['code'=>400,"msg"=>'修改失败']);
			}
    	}
		return $this->fetch();
	} 
	
	//赠送房券列表
	public function fqlist()
	{
		//查询出这个人所有赠送激活中房券
		$uid = session('user_info')['id'];
		$where['u.user_id'] = $uid;
		$where['u.start_time'] = ['<=',date("Y-m-d")]; 
		$list = db('userhouse')->alias('u') 
		->join('room_sort r','u.house_id = r.id','LEFT')
		->field('u.*,r.room_name,r.room_img')
		->where($where)->select();
		
		//查询出这个人所有赠送待激活中房券
		$uid = session('user_info')['id'];
		$where['u.user_id'] = $uid;
		$where['u.start_time'] = ['>',date("Y-m-d")]; 
		$list2 = db('userhouse')->alias('u') 
		->join('room_sort r','u.house_id = r.id','LEFT')
		->field('u.*,r.room_name,r.room_img')
		->where($where)->select();
		
		//待激活总数
		$count = db('userhouse')->where('user_id',$uid)->where('start_time','>',date("Y-m-d"))->sum('num');
		//已激活总数
		$count2 = db('userhouse')->where('user_id',$uid)->where('start_time','<=',date("Y-m-d"))->sum('num');
		
		$this->assign('list',$list); 
		$this->assign('list2',$list2); 
		$this->assign('count2',$count2); 
		$this->assign('count',$count); 
		return $this->fetch();
	}
	
	//个人资料
	public function member()
	{
		//查询出这个人个人信息
		$uid = session('user_info')['id'];
		$where['u.id'] = $uid; 
		$info = db('user')->alias('u')
		->join('userlevel e','u.level_id = e.id','LEFT')
		->join('user s','u.tj_emp = s.id','LEFT')
		->where($where)
		->field('u.nickname,u.id,u.user_name,u.mobile,u.birthday,u.sex,u.email,u.guoji,u.province,u.city,u.addres,s.user_name as ywy,e.name')
		->find();  
		$this->assign('info',$info); 
		if(request()->isPost()){
			$data = $_POST;  
			$res = db('user')->where('id',$uid)->update($data);
			if($res){
				return json(['code'=>200,"msg"=>'修改成功']);
			}else{
				return json(['code'=>400,"msg"=>'修改失败']);
			}
		}
		return $this->fetch();
	} 
	
	 //余额
    public function money()
	{
		$type = input('type') ? input('type') : 2;
		$uid = session('user_info')['id'];
		//查看个人信息
		$user=db('user')
       ->alias('a')->join('userlevel b','b.id=a.level_id',"LEFT")
	   ->field("a.balance,a.id,a.logo,a.mobile,b.name")
       ->where('openid',session('openid'))->find();
	    $this->assign('user',$user);	
		
		//明细列表
		$where['type'] = $type;
		if($type == 6){
			$where['type'] = ['in',[6,7]];
		}
		if($type == 1){
			$where['type'] = ['in',[1,9]];
		}
		if($type == 2){
			$where['type'] = ['in',[2,8]];
		}
		$where['user_id'] = $uid; 
		$list = db('consume')->alias('c')
		->join('user u','u.id = c.fill_id','LEFT') 
		->join('store s','c.store_id = s.id','LEFT')
		->where($where)->order('c.time desc')
		->field('c.msg,c.type,c.time,c.balance,s.store_name,u.mobile')
		->select(); 
		$this->assign('list',$list);
		$this->assign('type',$type);
		return $this->fetch();
	}
	
	public function zz()
	{
		if($_POST){
			$result = true;
			Db::startTrans();
			//验证验证码是否错误，过期
			$result = check_sendcode(session('user_info')['mobile'], $_POST['yzm'], '发送成功'); 
			if($result != 1) {
				return json(['code'=>400,"msg"=>'验证码错误或过期']);
			}  
			$mobile = input('mobile');
			$balance = $_POST['balance']; 
			//判断该用户是否存在
			$user = db('user')->where('mobile',$mobile)->find();
			if(!$user){
				return json(['code'=>400,"msg"=>'该会员不存在']);
			}
			//判断该用户的余额是否足够
			$money = $_POST['money'];
			$uid = session('user_info')['id'];
			if($money < $balance){
				return json(['code'=>400,"msg"=>'余额不足']);
			}
		   
		    //增加对方的余额
			$res1 = db('user')->where('id',$user['id'])->setInc('balance',$balance);
			if(!$res1) $result = false;
			
			//减少自己的余额 
			$res2 = db('user')->where('id',$uid)->setDec('balance',$balance);
			if(!$res2) $result = false;
			
			//打印自己转账的记录 
		    $res3 = consume_log(order_create_no(),$uid,'',$balance,$user['id'],4,'',now_time(),6,'','余额转账');
			if(!$res3) $result = false;
			//打印对方转入的记录 
			$res4 = consume_log(order_create_no(),$user['id'],'',$balance,$uid,4,'',now_time(),7,'','余额转入');
			if(!$res4) $result = false;
            if($result){
				Db::commit();
				return $this->success('转账成功'); 
			}else{
				Db::rollback();
				return $this->error('转账失败');
			}
		}
	}

    //佣金提现
    public function yj()
	{
		Db::startTrans();
		$res=[];
		$uid = session('user_info')['id'];
		$yj = input('yj');
		$data['balance']=['inc',$yj];
		$data['yj']=['dec',$yj];
		$res[] = db('user')->where('id',$uid)->update($data);
		//添加佣金提现记录
		$res[] = add_commission($uid,$yj,$uid,session('user_info')['level_id'],$yj,'佣金转余额',2);
		//添加佣金提现记录
		$res[] = consume_log(order_create_no(),$uid,'',$yj,'','',session('user_info')['level_id'],now_time(),9,'','佣金提现到余额','');
		if (in_array(false, $res)){
		    Db::rollback();
			return $this->error('提现失败');
		}else {
		    Db::commit();
			return $this->success('提现成功'); 
		} 
	}
}
